//
//  tabBarItem.swift
//  iPad_QQ
//
//  Created by 李善武 on 16/4/30.
//  Copyright © 2016年 李善武. All rights reserved.
//

import UIKit

class TabBarItem: UIButton {
    let ratio : CGFloat = 0.4
    override init(frame: CGRect) {
        super.init(frame: frame)
        imageView?.contentMode = .Center
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// 设置图片和文字在item中的位置信息
extension TabBarItem{
    // 重写图片的位置
    override func imageRectForContentRect(contentRect: CGRect) -> CGRect {
        if frame.width == frame.height{// 竖屏
            return bounds
        }else{// 横屏
            let imageW : CGFloat = self.frame.width * ratio
            let imageH : CGFloat = self.frame.height 
            let imageX : CGFloat = 0
            let imageY : CGFloat = 0
            return CGRectMake(imageX, imageY, imageW, imageH)
        }
    }
    
    // 重写标题的位置
    override func titleRectForContentRect(contentRect: CGRect) -> CGRect {
        if frame.width == frame.height{// 竖屏
           return CGRectZero
        }else{
            let titleW = frame.size.width * (1.0 - ratio)
            let titleH = frame.size.height
            let titleX : CGFloat = frame.size.width * ratio
            let titleY : CGFloat = 0.0
            return CGRectMake(titleX, titleY, titleW, titleH)
        }
    }
}







